package com.xisoft.base.entity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AbstractAggVO extends AggregatedBaseVO
{
    private BaseVO parentVO;
    private final Map< String,List< BaseVO > > childrenVO = new HashMap<>();

    @Override
    public BaseVO getParentVO( )
    {
        return this.parentVO;
    }

    @Override
    public void setParentVO(BaseVO baseVO)
    {
        this.parentVO = baseVO;
    }

    @Override
    public BaseVO[] getChildrenVO( )
    {
        return childrenVO.values().toArray(new BaseVO[0]);
    }

    @Override
    public void setChildrenVO(BaseVO[] baseVOs)
    {
        if (baseVOs == null || baseVOs.length == 0)
        { return; }
        for (BaseVO baseVO : baseVOs)
        {
            childrenVO.computeIfAbsent(baseVO.getClass().getName(), k -> new ArrayList<>());
            childrenVO.get(baseVO.getClass().getName()).add(baseVO);
        }
    }

    public < T extends BaseVO > T[] getChildrenVO(String name)
    {
        return (T[]) childrenVO.get(name).toArray();
    }
}
